//
//  CXLHomeController.m
//  WYNews
//
//  Created by 夜猫子 on 2017/3/18.
//  Copyright © 2017年 夜猫子. All rights reserved.
//

#import "CXLHomeController.h"
#import "CXLDetaileView.h"
#import "CXLNewListController.h"

@interface CXLHomeController ()<CXLDetaileViewDelegate,UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (nonatomic, weak) CXLDetaileView *detaileView;

@property (nonatomic, strong) UIPageViewController *pageViewController;

@end

@implementation CXLHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
    
    [self setPageViewController];
    
}

- (void)setupUI {
    //添加频道视图
    CXLDetaileView *detailView = [[CXLDetaileView alloc]init];
    detailView.detailDelegte = self;
    [self.view addSubview:detailView];
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.top.equalTo(self.view).offset(64);
    }];
    
    _detaileView = detailView;
}

#pragma mark - CXLDetaileViewDelegate
- (void)detailView:(NSInteger)index {
    
    CXLNewListController *newController = (CXLNewListController *)_pageViewController.viewControllers[0];
    
    NSInteger currenIndex = newController.index;
    //如果是同一个index,直接返回
    if (currenIndex == index) {
        return;
    }
    //默认右滑
    UIPageViewControllerNavigationDirection direction= UIPageViewControllerNavigationDirectionForward;
    //如果index小于当前子控制器index,则左往右滑
    if (index < currenIndex) {
        direction =UIPageViewControllerNavigationDirectionReverse;
    }
    
    //重新设置子控制器
    CXLDetailsModel *model = [_detaileView.modelArr objectAtIndex:index];
    
    CXLNewListController *newList = [[CXLNewListController alloc] initWithIndex:index andModel:model];
    
    //确定滑动方向
    [_pageViewController setViewControllers:@[newList] direction:direction animated:YES completion:nil];
}

#pragma mark - 添加新闻控制器
- (void)setPageViewController {
    //创建UIPageViewController
    UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey:@0}];
    _pageViewController = pageViewController;
    //将UIPageViewController添加到控制器
    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];
    [pageViewController didMoveToParentViewController:self];
    
    //设置UIPageViewController的位置
    [pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_detaileView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    //添加子控制器
    CXLDetailsModel *model = [_detaileView.modelArr objectAtIndex:0];
    CXLNewListController *newListController = [[CXLNewListController alloc]initWithIndex:0 andModel:model];
    NSArray *controllers = @[newListController];
    [pageViewController setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    //实现数据源
    pageViewController.dataSource = self;
    pageViewController.delegate = self;
}

#pragma mark - UIPageViewControllerDataSource
/**
 返回前一个控制器
 
 @param viewController viewController 当前显示的控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(CXLNewListController *)viewController{
    NSInteger index = viewController.index;
    if (index == 0) {
        return nil;
    }
    CXLDetailsModel *model = [_detaileView.modelArr objectAtIndex:index - 1];
    CXLNewListController *newController = [[CXLNewListController alloc] initWithIndex:index -1 andModel:model];
    
    return newController;
    
}
/**
 返回后一个控制器
 
 @param viewController viewController 当前显示的控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(CXLNewListController *)viewController{
    NSInteger index = viewController.index;
    if (index == _detaileView.modelArr.count - 1) {
        return nil;
    }
    
    CXLDetailsModel *model = [_detaileView.modelArr objectAtIndex:index + 1];
    
    CXLNewListController *newController = [[CXLNewListController alloc] initWithIndex:index + 1 andModel:model];
    
    return newController;
    
}

/**
 将要发生动画时调用的方法
 
 @param pageViewController 当前的pageViewController
 @param pendingViewControllers 将要展示的UIViewController的数组（只有一个元素）
 */
#pragma mark - UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{
    
    CXLNewListController *nextVC = (CXLNewListController *)pendingViewControllers[0];
    
    [_detaileView setIsSelegte:nextVC.index];
}


/**
 动画完成时调用的方法
 
 @param pageViewController 当前的pageViewController
 @param previousViewControllers 前一个UIViewController的数组（只有一个元素）
 */
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    
    CXLNewListController *cuuret = (CXLNewListController *)pageViewController.viewControllers[0];
    
    [_detaileView setIsSelegte:cuuret.index];
}



@end
